//package com.kehutong.admin.security;
//import java.util.ArrayList;
//import java.util.List;
//
//import javax.servlet.http.HttpServletRequest;
//
//import org.coraframework.authz.exception.UnloginException;
//import org.coraframework.inject.Singleton;
//import org.coraframework.json.JSONObject;
//import org.coraframework.mvc.http.HttpReqMsg;
//import org.coraframework.mvc.http.interceptor.Interceptor;
//import org.coraframework.orm.Entity;
//import org.coraframework.orm.Pool;
//import org.coraframework.orm.jdbc.JdbcSession;
//import org.coraframework.upload.FileItem;
//import org.coraframework.util.Ioc;
//import org.coraframework.util.Objects;
//
//import com.kehutong.admin.entity.Log;
//import com.kehutong.admin.enums.LogType;
//import com.kehutong.common.util.Token;
//
//
//
///**
// * 系统拦截器
// * @author ThinkGem
// * @version 2013-6-6
// */
//@Singleton
//public class LogInterceptor extends Interceptor {
//	
//	private static JdbcSession jdbcSession;
//	
//	static {
//		jdbcSession = Ioc.getInstance(JdbcSession.class);
//	}
//
//	@Override
//	public Object intercept(HttpReqMsg req) throws Exception {
//		Throwable ex = null;
//		try {
//			beforeInvoker(req);
//			return req.invoke();
//		} catch (UnloginException e) {
//			throw e;
//		} catch (Throwable th) {
//			throw new Exception(ex = th);
//		} finally {
//			afterInvoker(req, ex);
//		}
//	}
//	
//	private void beforeInvoker(HttpReqMsg req) {
//		
//	}
//	
//	private void afterInvoker(HttpReqMsg rq, Throwable ex) throws Exception {
//		Token token = getToken(rq.getAttach());
//		if (Objects.isNull(token)) {
//			return;
//		}
//		
//		HttpServletRequest request = rq.getHttpServletRequest();
//		String requestRri = request.getRequestURI();
//		String uriPrefix = request.getContextPath();
//		
//		if ((requestRri.startsWith(uriPrefix) && 
//				(requestRri.endsWith("/save") || 
//				requestRri.endsWith("/delete") || 
//				requestRri.endsWith("/import") || 
//				requestRri.endsWith("/update") || 
//				requestRri.endsWith("/updateSort"))) || 
//				ex != null){
//			
//			Log log = Pool.newInstance(Log.class);
//			log.setCompanyNo(token.getCompanyNo());
//			log.setCompanyName(token.getCompanyName());
//			log.setType(ex == null ? LogType.ACCESS : LogType.EXCEPTION);
//			log.setRemote_addr(getRemoteAddr(request));
//			log.setUser_agent(request.getHeader("user-agent"));
//			log.setRequest_uri(request.getRequestURI());
//			log.setMethod(request.getMethod());
//			log.setParams(getParams(rq.getParams()));
//			log.setCreate_by(token.getUuid());
//			log.setCreate_name(token.getUsername());
//			log.setException(ex != null ? ex.toString() : null);
//			
//			jdbcSession.insert(log);
//		}
//		
////		logger.debug("最大内存: {}, 已分配内存: {}, 已分配内存中的剩余空间: {}, 最大可用内存: {}", 
////				Runtime.getRuntime().maxMemory(), Runtime.getRuntime().totalMemory(), Runtime.getRuntime().freeMemory(), 
////				Runtime.getRuntime().maxMemory()-Runtime.getRuntime().totalMemory()+Runtime.getRuntime().freeMemory()); 
//		
//	}
//	/**
//     * 获得用户远程地址
//     */
//    private String getRemoteAddr(HttpServletRequest request){
//        String remoteAddr = request.getHeader("X-Real-IP");
//        if (Objects.nonEmpty(remoteAddr)) {
//            remoteAddr = request.getHeader("X-Forwarded-For");
//        }else if (Objects.nonEmpty(remoteAddr)) {
//            remoteAddr = request.getHeader("Proxy-Client-IP");
//        }else if (Objects.nonEmpty(remoteAddr)) {
//            remoteAddr = request.getHeader("WL-Proxy-Client-IP");
//        }
//        return remoteAddr != null ? remoteAddr : request.getRemoteAddr();
//    }
//    
//    private String getParams(Object[] params) {
//    	if (Objects.isEmpty(params)) {
//    		return null;
//    	}
//    	
//    	List<String> array = new ArrayList<>();
//    	for (Object param : params) {
//    		if (param instanceof Token) {
//    			continue;
//    		} 
//    		
//    		if (param instanceof FileItem) {
//    			continue;
//    		}
//    		
//    		if (param instanceof Entity) {
//    			return param.toString();
//    		} 
//
//    		if (param instanceof JSONObject) {
//    			((JSONObject) param).remove("token");
//    			return param.toString();
//    		}
//    		
//    		array.add(param.toString());
//    	}
//    	
//    	return String.join(", ", array);
//    }
//    
//    private Token getToken(JSONObject jsonObject) {
//    	if (Objects.isEmpty(jsonObject)) {
//    		return null;
//    	}
//    	
//    	return jsonObject.getObject("token", Token.class);
//    }
//
//}
